<?php
/**
 * 手机短信平台controller
 * author:ty
 * date:2017/08/24
 */
namespace App\Http\Controllers;

require_once dirname(__DIR__) .'/Util/api_sdk/vendor/autoload.php';
use App\Http\Util\api_sdk\lib\Core\Config;
use App\Http\Util\api_sdk\lib\Core\Profile\DefaultProfile;
use App\Http\Util\api_sdk\lib\Core\DefaultAcsClient;
use App\Http\Util\api_sdk\lib\Api\Sms\Request\V20170525\SendSmsRequest;
use App\Http\Util\api_sdk\lib\Api\Sms\Request\V20170525\QuerySendDetailsRequest;
use Illuminate\Support\Facades\Input;
Config::load();

class MscodeController extends Controller
{
    /**初始化配置*/
    public function __construct()
    {
        $accessKeyId = env('ACCESSKEYID');
        $accessKeySecret = env('ACCESSKEYSECRET');

        $product = "Dysmsapi";// 短信API产品名

        $domain = "dysmsapi.aliyuncs.com";// 短信API产品域名

        $region = "cn-hangzhou"; // 暂时不支持多Region

        $endPointName = "cn-hangzhou";// 服务结点

        $profile = DefaultProfile::getProfile($region, $accessKeyId, $accessKeySecret);// 初始化用户Profile实例

        DefaultProfile::addEndpoint($endPointName, $region, $product, $domain);// 增加服务结点

        $this->acsClient = new DefaultAcsClient($profile);// 初始化AcsClient用于发起请求
    }

    /**
     * 发送短信范例
     *
     * @param string $signName <p>
     * 必填, 短信签名，应严格"签名名称"填写，参考：<a href="https://dysms.console.aliyun.com/dysms.htm#/sign">短信签名页</a>
     * </p>
     * @param string $templateCode <p>
     * 必填, 短信模板Code，应严格按"模板CODE"填写, 参考：<a href="https://dysms.console.aliyun.com/dysms.htm#/template">短信模板页</a>
     * (e.g. SMS_0001)
     * </p>
     * @param string $phoneNumbers 必填, 短信接收号码 (e.g. 12345678901)
     * @param array|null $templateParam <p>
     * 选填, 假如模板中存在变量需要替换则为必填项 (e.g. Array("code"=>"12345", "product"=>"阿里通信"))
     * </p>
     * @param string|null $outId [optional] 选填, 发送短信流水号 (e.g. 1234)
     * @return stdClass
     */
    public function sendSms($signName, $templateCode, $phoneNumbers, $templateParam = null, $outId = null) {

        // 初始化SendSmsRequest实例用于设置发送短信的参数
        $request = new SendSmsRequest();

        // 必填，设置雉短信接收号码
        $request->setPhoneNumbers($phoneNumbers);

        // 必填，设置签名名称
        $request->setSignName($signName);

        // 必填，设置模板CODE
        $request->setTemplateCode($templateCode);

        // 可选，设置模板参数
        if($templateParam) {
            $request->setTemplateParam(json_encode($templateParam));
        }

        // 可选，设置流水号
        if($outId) {
            $request->setOutId($outId);
        }
        // 发起访问请求
        $acsResponse = $this->acsClient->getAcsResponse($request);

        // 打印请求结果
        // var_dump($acsResponse);

        return $acsResponse;

    }

    /**
     * 查询短信发送情况范例
     *
     * @param string $phoneNumbers 必填, 短信接收号码 (e.g. 12345678901)
     * @param string $sendDate 必填，短信发送日期，格式Ymd，支持近30天记录查询 (e.g. 20170710)
     * @param int $pageSize 必填，分页大小
     * @param int $currentPage 必填，当前页码
     * @param string $bizId 选填，短信发送流水号 (e.g. abc123)
     * @return stdClass
     */
    public function queryDetails($phoneNumbers, $sendDate, $pageSize = 10, $currentPage = 1, $bizId=null)
    {
        $request = new QuerySendDetailsRequest();// 初始化QuerySendDetailsRequest实例用于设置短信查询的参数

        $request->setPhoneNumber($phoneNumbers); // 必填，短信接收号码

        $request->setBizId($bizId);// 选填，短信发送流水号

        $request->setSendDate($sendDate);// 必填，短信发送日期，支持近30天记录查询，格式Ymd

        $request->setPageSize($pageSize);// 必填，分页大小

        $request->setCurrentPage($currentPage);// 必填，当前页码

        $acsResponse = $this->acsClient->getAcsResponse($request);// 发起访问请求

        // 打印请求结果
        return $acsResponse;
    }
    /**
     * 图片验证码校验
     * @code 用户输入code
     */
    public function checkcode()
    {
        $code = trim(Input::get('code'));
        if($code!=session('codeSession')){
            $res = ['messages'=>'错误','code'=>'400','code'=>$code,'session'=>session('codeSession')];
            echo json_encode($res);
        }else{
            $res = ['messages'=>'通过','code'=>'200','code'=>$code,'session'=>session('codeSession')];
            echo json_encode($res);
        }
    }

    /**
     * 手机验证短信获取
     * @phone 手机号
     * @type 【1=>注册,2=>密码找回，3=>修改密码】
     */
    public function mscode()
    {
        $phone = trim(Input::get('phone'));
        $type = trim(Input::get('type'));
        $signName = '芈租界';
        $sendDate = date('Ymd',time());
        if($type==1){
            $templateCode = 'SMS_102225074';
        }
        if($type==2){
            $templateCode = 'SMS_101780005';
        }
        if($type==3){
            $templateCode = 'SMS_102260083';
        }
        $rand_number = rand ( 100000, 999999 );
        /**短信发送*/
        $res = $this->sendSms($signName, $templateCode, $phone, Array("code"=>$rand_number, "product"=>"dsd"),"123");
        if($res->Code!='OK'){
            $data = ['code' => '400','messages'=>'发送失败!'];
           return  json_encode($data);
        }
        if($res){
            setcookie("phonemsg", $rand_number,time()+1200);
            $result = [
                'Sendcode' => $rand_number,
                'phone' => $phone
            ];
            $data = ['code' => 200,'messages'=>'发送成功!','result' => $result];
            echo json_encode($data);
        }else{
            $data = ['code' => '400','messages' => '没有返回值!','result' => ''];
            echo json_encode($data);
        }

    }

}


